<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\OrderProduct;
use app\common\model\Product;
use app\common\model\ProductSpecValue;
use app\common\model\SeckillProduct;
use app\common\model\Lntegral;
use app\common\model\LntegralSpecValue;

class Operation
{
    public function handle($data)
    {
        $list = OrderProduct::where('order_id', $data)->field('pid,num,spec,shoptype,seckill_id')->select();

        foreach ($list as $v) {
            if($v['shoptype']=='shop'){
                ProductSpecValue::where(['pid'=>$v->pid,'sku_id'=>$v->spec])->dec('stock', $v->num)->update();
                Product::where('id', $v->pid)->inc('sales', $v->num)->update();
                Product::where('id', $v->pid)->dec('stock', $v->num)->update();
            }else if($v['shoptype']=='seckill'){
                if($v['seckill_id'] != 0){
                    SeckillProduct::where(['seckill_id'=>$v->seckill_id,'product_id'=>$v->pid])->inc('sales', $v->num)->update();
                    SeckillProduct::where(['seckill_id'=>$v->seckill_id,'product_id'=>$v->pid])->dec('stock', $v->num)->update();
                }
            }else{
                LntegralSpecValue::where(['pid'=>$v->pid,'sku_id'=>$v->spec])->dec('stock', $v->num)->update();
                Lntegral::where('id', $v->pid)->inc('sales', $v->num)->update();
                Lntegral::where('id', $v->pid)->dec('stock', $v->num)->update();
            }
        }
    }


}